<?php defined('SYSPATH') or die('No direct script access.');

abstract class Controller_Admin_Base extends Controller
{
    protected $title  = 'Teamdesk.ru';
	protected $layout = 'admin/layout_tpl';
    protected $need_auth = true;
    protected $message = null;
    
    protected function is_need_auth()
    {
        return $this->need_auth;
    }
    
    protected function add_message($message)
    {
        $this->message = $message;   
    }
    
    public function before()
    {
        parent::before();
        
        $this->_detect_company();
        
        if (!Model_Auth::instance()->logged_in() && $this->is_need_auth()) {
            Request::current()->redirect('http://'.Model_Main::get_base_domain().'/login');    
        }
    }
    
    protected function _detect_company()
    {
        $host  = $_SERVER['HTTP_HOST'];
        $parts = explode('.', $host);
        
        if (count($parts) == 3) {
            $domain = $parts[0];
            $company = ORM::factory('company')->get_company_by_domain($domain);
            $company->set_as_current();
        }
    }
	
	public function print_template($title, $content, $active_top_nav = 'mailbox', $disable_top_panel = false)
	{
        $this->layout = new View($this->layout);

        $mailboxes = ORM::factory('mailbox')->get_mailboxes();
		$this->layout->title       = $title.' - '.$this->title;
		$this->layout->content     = $content;
        $this->layout->message     = $this->message;
        $this->layout->mailboxes   = $mailboxes;
        $this->layout->active_top_nav = $active_top_nav;
        $this->layout->disable_top_panel = $disable_top_panel;
        
        if (isset($_COOKIE['count_closed']) && isset($_COOKIE['count_other']) && isset($_COOKIE['count_replies'])) {
            $count_closed  = $_COOKIE['count_closed'];
            $count_other   = $_COOKIE['count_other'];
            $count_replies = $_COOKIE['count_replies'];
        } else {
            $ids = array();
            foreach ($mailboxes as $mailbox) {
                $ids[] = $mailbox->id;
            }
            $closed = $other = $replies = array();
            $count_closed = $count_other = $count_replies = 0;
            if (count($ids)) {
                $closed = ORM::factory('ticket')
                            ->where('mailbox_id', 'IN', $ids)
                            ->where('status', '=', Model_Ticket::STATUS_CLOSED)
                            ->find_all();
                $other = ORM::factory('ticket')
                            ->where('mailbox_id', 'IN', $ids)
                            ->where('status', '!=', Model_Ticket::STATUS_CLOSED)
                            ->find_all();
                $ids = array();
                foreach ($closed as $ticket) {
                    $ids[] = $ticket->id;    
                }
                foreach ($other as $ticket) {
                    $ids[] = $ticket->id;    
                }
                if (count($ids)) {
                    $replies = ORM::factory('reply')
                                ->where('ticket_id', 'IN', $ids)
                                ->find_all();
                }
                
                $count_closed = count($closed);
                $count_other  = count($other);
                $count_replies = count($replies);  
            }
            setcookie('count_closed', $count_closed, time()+24*60*60, '/');
            setcookie('count_other', $count_other, time()+24*60*60, '/');  
            setcookie('count_replies', $count_replies, time()+24*60*60, '/');
        }
        $this->layout->count_vars = array(
            'closed'  => $count_closed,
            'other'   => $count_other,
            'replies' => $count_replies
        );
		
		$this->response->body($this->layout);
	}

}
